package com.guaoran.interview.in23.collection;

/**
 * @author : 孤傲然
 * @description : ThreadLocalDemo
 * <p>
 * 2 的N次幂  2的平方
 * @date :2023/7/3 12:17
 */
public class ThreadLocalDemo {
    static ThreadLocal threadLocal = new ThreadLocal();
    static ThreadLocal threadLocal2 = new ThreadLocal();

    private static final int HASH_INCREMENT = 0x61c88647;

    public static void magicHash(int size) {
        int hashcode = 0;
        for (int i = 0; i < size; i++) {
            hashcode = i * HASH_INCREMENT + HASH_INCREMENT;
            System.out.print((hashcode & (size - 1)) + " ");
        }
        System.out.println();
    }

    public static void main(String[] args) {
        new Thread(()->{
            threadLocal.set(1);
            threadLocal2.set(2);
            Object o = threadLocal.get();
            System.out.println(o);
            Object o1 = threadLocal2.get();
            System.out.println(o1);
            threadLocal.remove();
        },"guAoRan").start();

//        test();
//        for (int i = 0; i < (1 << 5 + 1); i++) {
//            if (isPower(i)) {
//                magicHash(i);
//            }
//        }
//
//        threadLocal.set(null);
    }

    public static void test() {
        for (int i = 0; i < (1 << 5 + 1); i++) {
            if (isPower(i) && isPower2(i)) {
                System.out.print(i + "\t");
            }

        }
    }

    // 2的次方 2的幂次方
    public static boolean isPower(int n) {
        if (n < 1) return false;
        int m = 1;
        while (m < n) {
            m = m << 1;
        }
        if (m == n) return true;
        return false;
    }

    public static boolean isPower2(int n) {
        if (n < 1) return false;
        return (n & (n - 1)) == 0;
    }
}
